﻿/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.ViewModels.Controls
{
    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.Gui;

    public class PracticePopupViewModel : LocalisedItemViewModel
    {
        #region Constructors

        public PracticePopupViewModel(PatientEditorViewModel parent, IErrorService errorService, IPatientService service, PatientCardDto patient, bool isAsync = true)
            : base(parent, errorService, service, patient, isAsync)
        {
        }

        #endregion Constructors

        #region Methods

        protected override void Create()
        {
            var practice = BuildPractice();
            if (!this.Service.Exists(practice))
            {
                this.Service.CreateAndBind(practice, this.Patient);
                this.ParentViewModel.RefreshPractice();
            }
            else
            {
                var toCreate = this.Service.GetFirstPractice(this.Name);
                this.Service.Bind(toCreate, this.Patient);
                this.ParentViewModel.RefreshPractice();
                //ViewService.MessageBox.Information(Messages.Msg_InsuranceExists)
                this.ParentViewModel.UiMessage = UiMessageDto.Warn(Messages.Msg_PracticeExists);
            }
        }

        private PracticeDto BuildPractice()
        {
            var practice = new PracticeDto()
            {
                Name = this.Name,
                Phone = this.Phone,
                Address = new AddressDto()
                {
                    BoxNumber = this.BoxNumber,
                    City = this.City,
                    PostalCode = this.PostalCode,
                    Street = this.Street,
                    StreetNumber = this.StreetNumber,
                }
            };
            return practice;
        }

        #endregion Methods
    }
}